package com.cn.xiaonuo.slbfinances.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.payconfig.entity.SlPayChannel;
import com.cn.xiaonuo.slbfinances.payconfig.entity.SlPayType;
import com.cn.xiaonuo.slbfinances.payconfig.enums.SlbEnum;
import com.cn.xiaonuo.slbfinances.payconfig.mapper.SlPayChannelMapper;
import com.cn.xiaonuo.slbfinances.payconfig.mapper.SlPayTypeMapper;
import com.cn.xiaonuo.slbfinances.payconfig.param.SlPayChannelParam;
import com.cn.xiaonuo.slbfinances.payconfig.service.SlbPayChannelService;
import com.cn.xiaonuo.tcrfinance.payconfig.enums.HpcEnum;
import com.cn.xiaonuo.tcrfinance.payconfig.param.PayChannelParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.xmlbeans.impl.jam.xml.TunnelledException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author luyangyang
 */
@Slf4j
@Service
public class SlbPayChannelServiceImpl implements SlbPayChannelService {


    @Autowired
    private SlPayChannelMapper slPayChannelMapper;

    @Autowired
    private SlPayTypeMapper slPayTypeMapper;

    @Autowired
    private Environment env;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage pagePayChannel(SlPayChannelParam param) {
        BusinessMessage message = new BusinessMessage();
        Integer pageNo = param.getPageNo();
        Integer pageSize = param.getPageSize();
        if (pageNo == null || pageNo.intValue() < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize.intValue() < 1) {
            pageSize = 10;
        }
        if (ObjectUtils.isEmpty(param.getPayTypeId())) {
            message.setMessage("支付方式ID不能为空");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<SlPayChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SlPayChannel::getPayTypeId, param.getPayTypeId());
        wrapper.eq(SlPayChannel::getDeleted, SlbEnum.DELETED_OK.getCode());
        List<SlPayChannel> payChannels = slPayChannelMapper.selectList(wrapper);
        PageInfo<SlPayChannel> pageInfo = new PageInfo<>(payChannels);
        message.setData(pageInfo);
        message.setMessage(Status.success);
        message.setSuccess(true);
        message.setCode(Status.successCode);
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage addPayChannel(SlPayChannel param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getPayTypeId())) {
            message.setMessage("使用场景不能为空");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        if (ObjectUtils.isEmpty(param.getPayCode())) {
            message.setMessage("支付方式不能为空");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        slPayChannelMapper.insert(param);
        message.setMessage(Status.success);
        message.setSuccess(true);
        message.setCode(Status.successCode);
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage updatePayChannel(SlPayChannel param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getId())) {
            message.setMessage("支付渠道ID不能为空");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        try {
            SlPayChannel payChannel = slPayChannelMapper.selectById(param.getId());
            if (ObjectUtils.isNotEmpty(payChannel)) {
                slPayChannelMapper.updateById(param);
            }
            message.setMessage(Status.success);
            message.setSuccess(true);
            message.setCode(Status.successCode);
        } catch (Exception e) {
            message.setMessage("操作失败!");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage deletePayChannel(Integer id) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(id)) {
            message.setMessage("支付渠道ID不能为空!");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        SlPayChannel payChannel = slPayChannelMapper.selectById(id);
        if (ObjectUtils.isNotEmpty(payChannel)) {
            if (payChannel.getDeleted().equals(SlbEnum.DELETED.getCode())) {
                message.setMessage("改渠道已被删除，请勿重复提交!");
                message.setSuccess(false);
                message.setCode(Status.failCode);
                return message;
            }
            payChannel.setDeleted(SlbEnum.DELETED.getCode());
            slPayChannelMapper.updateById(payChannel);
            message.setMessage(Status.success);
            message.setSuccess(true);
            message.setCode(Status.successCode);
            return message;
        } else {
            message.setMessage(Status.fail);
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage payChannelDetail(Integer id) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(id)) {
            message.setMessage("支付渠道ID不能为空!");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        SlPayChannel payChannel = slPayChannelMapper.selectById(id);
        message.setData(payChannel);
        message.setMessage(Status.success);
        message.setSuccess(true);
        message.setCode(Status.successCode);
        return message;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage updatePayChannelState(PayChannelParam param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getPayChannelId()) || ObjectUtils.isEmpty(param.getPayTypeId())) {
            message.setMessage("支付方式ID不能为空!");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        try {
            SlPayType payType = slPayTypeMapper.selectById(param.getPayTypeId());
            if (ObjectUtils.isNotEmpty(payType)) {

                SlPayChannel payChannel = slPayChannelMapper.selectById(param.getPayChannelId());
                if (param.getStatus().equals(HpcEnum.STATUS.getCode())) {
                    payChannel.setStatus(param.getStatus());
                    slPayChannelMapper.updateById(payChannel);
                    //关闭支付渠道时需关闭支付方式
                    payType.setStatus(param.getStatus());
                    slPayTypeMapper.updateById(payType);
                }
                if (param.getStatus().equals(SlbEnum.STATUS_OK.getCode())) {
                    LambdaQueryWrapper<SlPayChannel> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(SlPayChannel::getPayTypeId,param.getPayTypeId());
                    wrapper.eq(SlPayChannel::getDeleted,SlbEnum.DELETED_OK.getCode());
                    wrapper.eq(SlPayChannel::getStatus,SlbEnum.STATUS_OK.getCode());
                    SlPayChannel channel = slPayChannelMapper.selectOne(wrapper);
                    if (ObjectUtils.isNotEmpty(channel)){
                        message.setMessage("请关闭正在使用的渠道后重试!");
                        message.setSuccess(false);
                        message.setCode(Status.failCode);
                        return message;
                    }
                    SlPayChannelParam channelParam = new SlPayChannelParam();
                    channelParam.setPayChannelId(payChannel.getId());
                    channelParam.setStatus(param.getStatus());
                    payChannel.setStatus(param.getStatus());
                    slPayChannelMapper.updateById(payChannel);
                }
            }
            message.setMessage(Status.success);
            message.setSuccess(true);
            message.setCode(Status.successCode);
        }catch (Exception e){
            message.setMessage(Status.fail);
            message.setSuccess(false);
            message.setCode(Status.failCode);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }
}
